
<?php
require_once 'model/db.php';

class pagination {
    //put your code here
    public function display($page, $total, $file) {
        $pages = $total / PER_PAGE;
        $html = '';
        $count = 1;
        $prev = $page - 1;
        $next = $page + 1;
        $first = 1;
        $html = "<a href='$file?page=$page'>$page</a>";
        while ($count < PER_PAGE / 2) {
            if ($page - $count >= 1) {
                $li = $page - $count;
                $html = "<a href='$file?page=$li' style=\"text-decoration: none\">$li</a>" . ' - '.$html;
            }
            if ($page + $count <= $pages) {
                $li = $page + $count;
                $html .= ' - ' . "<a href='$file?page=$li' style=\"text-decoration: none\">$li</a>";
            }
            $count++;
        }
        if ($prev > 0 && $next <= $pages)
            return "<a href='$file?page=$first' style=\"text-decoration: none\">First</a>" . ' - '
                    . "<a href='$file?page=$prev' style=\"text-decoration: none\">Previous</a>" . ' - '
                    . $html . ' - '
                    . "<a href='$file?page=$next' style=\"text-decoration: none\">Next</a>" . ' - '
                    . "<a href='$file?page=$pages' style=\"text-decoration: none\">Last</a>";
        else if ($prev <= 0)
            return "<a href='$file?page=$first' style=\"text-decoration: none\">First</a>" . ' - '
                    . "<a href='$file?page=$prev'></a>" . ' '
                    . $html . ' - '
                    . "<a href='$file?page=$next' style=\"text-decoration: none\">Next</a>" . ' - '
                    . "<a href='$file?page=$pages' style=\"text-decoration: none\">Last</a>";
        else
            return "<a href='$file?page=$first' style=\"text-decoration: none\">First</a>" . ' - '
                    . "<a href='$file?page=$prev' style=\"text-decoration: none\">Previous</a>" . ' - '
                    . $html . ' - '
                    . "<a href='$file?page=$next'></a>" . ' '
                    . "<a href='$file?page=$pages' style=\"text-decoration: none\">Last</a>";
    }
    
}
?>
